package com.zzld.modules.autoinn.action;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.jsoup.helper.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageInfo;
import com.zzld.core.utils.MD5Util;
import com.zzld.core.utils.MessageUtils;
import com.zzld.modules.autoinn.entity.Facilitator;
import com.zzld.modules.autoinn.service.IFacilitatorService;
import com.zzld.modules.common.dto.CommonJSON;
import com.zzld.modules.maintenance.service.IMatnexpensepathService;
import com.zzld.modules.maintenance.service.IMatnexpenseService;

@Controller
@RequestMapping("/autoinn/wxfacilitator/setting")
public class WXFacilitatorSettingController {

	@Autowired
	private IFacilitatorService faciliatorService;
	
	@Autowired
	private IMatnexpenseService matnexpenseService;
	
	@Autowired
	private IMatnexpensepathService matnexpensepathService;
	
	@RequestMapping(value="/del",method = RequestMethod.POST)
	@ResponseBody
	public CommonJSON del(HttpServletRequest request){
		CommonJSON json = new CommonJSON();
		String id = request.getParameter("id");
		Facilitator fa =(Facilitator)request.getSession().getAttribute("fa");
		try{
		if(!StringUtil.isBlank(id)){
		    if(fa.getType()==0){
		    	if(faciliatorService.deleteByPrimaryKey(id)>0){
		    		json.setMessage(MessageUtils.EDIT_SUCCESS);
		    		json.setSuccess(true);
		    	}
		    }else{
		    	json.setMessage("您当前用户不具备删除权限!");
		    	json.setSuccess(false);
		    }
		}
		}catch(Exception e){
			json.setMessage(MessageUtils.DELETE_WARNING);
			json.setSuccess(false);
		}
		return json;
	}
	
	@RequestMapping(value="showpass",method=RequestMethod.POST)
	@ResponseBody
	public CommonJSON showpass(HttpServletRequest request){
		CommonJSON json = new CommonJSON();
		try{
			Facilitator fa = (Facilitator)request.getSession().getAttribute("fa");
			if(fa!=null&&request.getParameter("password")!=null){
				fa.setPassword(MD5Util.encoderByMd5With32Bit(request.getParameter("password").toString()));
			}
			if(faciliatorService.updateByPrimaryKeySelective(fa)>0){
				json.setMessage(MessageUtils.EDIT_SUCCESS);
				json.setSuccess(true);
			}
		}catch(Exception e){
			json.setMessage(MessageUtils.EDIT_WARING);
			json.setSuccess(false);
		}
		return json;
	}

	@RequestMapping("/list")
	@ResponseBody
	public CommonJSON list(HttpServletRequest request) {
		CommonJSON json = new CommonJSON();
		Map<String, Object> map = new HashMap<String, Object>();
		try{
		Facilitator fa =(Facilitator) request.getSession().getAttribute("fa");
			
	

		map.put("name", request.getParameter("name"));
		map.put("startime", request.getParameter("starinnertime"));
		map.put("endtime", request.getParameter("endinnertime"));
		map.put("sort", request.getParameter("sort"));
		map.put("order", "ASC");
		map.put("storeid",fa.getStoreid());
		}catch(Exception e){
			e.printStackTrace();
		}
		
		
		Integer pageNum = Integer.parseInt(request.getParameter("page"));
		Integer pageSize = Integer.parseInt(request.getParameter("rows"));
		
		PageInfo page = faciliatorService.list(map, pageNum, pageSize);
		json.setRows(page.getList());
		json.setTotal(page.getTotal());
		
		return json;
	}
	
	@RequestMapping("/maten/list")
	@ResponseBody
	public CommonJSON matlist(HttpServletRequest request){
		CommonJSON json = new CommonJSON();
		Map<String, Object> map = new HashMap<String, Object>();

		map.put("phonenum", request.getParameter("phonenum"));
		map.put("storename", request.getParameter("storename"));
		map.put("exstartime", request.getParameter("exstartime"));
		map.put("exendtime", request.getParameter("exendtime"));

		Facilitator fa =(Facilitator) request.getSession().getAttribute("fa");
		map.put("storeid", fa.getStoreid());
		
		Integer PageNum = Integer.parseInt(request.getParameter("page"));
		Integer PageSize = Integer.parseInt(request.getParameter("rows"));

		PageInfo pageinfo = matnexpensepathService.listbyfa(map, PageNum, PageSize);
		json.setRows(pageinfo.getList());
		json.setTotal(pageinfo.getTotal());
		return json;
	}

}
